﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using GTServices.Model.Models;
using GTServices.Model;
using System.Text.RegularExpressions;
using System.Windows;

namespace GTServices.WPF.Client.ViewModels
{
    public class DirectoryFilterViewModel : Screen
    {
        public IList<String> DirectoryFilters { get; set; }
        
        public String Filter { get; set; }

        public String SelectedItem { get; set; }

    

        public DirectoryFilterViewModel()
        {
            this.DisplayName = "Directory Filters";
            this.DirectoryFilters = GTApplication.Instance.FilesModel.DirectoryFilters;
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
            GTApplication.Instance.FilesModel.SaveDirectoryFilters(); 
        }

        public void Remove()
        {
            if (DirectoryFilters.Contains(SelectedItem))
            {
                DirectoryFilters.Remove(SelectedItem); 
            }
        }

        public void Add()
        {
            if (!String.IsNullOrEmpty(Filter) && IsValidRegex(Filter))
            {
                this.DirectoryFilters.Add(Filter);
                this.Filter = "";
                NotifyOfPropertyChange(() => Filter); 
            }
        }

        public bool IsValidRegex(String pattern)
        {
            try
            {
                Regex.Match("", pattern);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("Invalid regex pattern");
                return false;
            }
        }
    }
}
